package com.jumi.microservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jumi.microservice.domain.GoodsDO;
import com.jumi.microservice.domain.GoodsFootprintDO;
import com.jumi.microservice.domain.GoodsFootprintVO;
import com.jumi.microservice.mapper.GoodsFootprintMapper;
import com.jumi.microservice.mapper.GoodsMapper;
import com.jumi.microservice.service.GoodsFootprintService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class GoodsFootprintServiceImpl  extends ServiceImpl<GoodsFootprintMapper, GoodsFootprintDO> implements GoodsFootprintService {

    @Autowired
    private GoodsFootprintMapper goodsFootprintMapper;

    /**
     * 商品管理mapper组件
     */
    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public void FootprintAdd(Long memberId, Long goodsId) {
        LambdaQueryWrapper<GoodsFootprintDO> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(GoodsFootprintDO::getGoodsId,goodsId);
        wrapper.eq(GoodsFootprintDO::getMemberId,memberId);
        GoodsFootprintDO DO = goodsFootprintMapper.selectOne(wrapper);
        GoodsFootprintDO goodsFootprintDO = new GoodsFootprintDO();
        if(DO == null){
            goodsFootprintDO.setMemberId(memberId);
            goodsFootprintDO.setGoodsId(goodsId);
            goodsFootprintMapper.insert(goodsFootprintDO);
        }else{
            goodsFootprintMapper.FootprintupdateTime(DO.getId());
        }
    }

    @Override
    public void FootprintDelete(Long id) {
        goodsFootprintMapper.deleteById(id);
    }

    @Override
    public List<GoodsFootprintVO> FootprintList(Long memberId) {
        LambdaQueryWrapper<GoodsFootprintDO> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(GoodsFootprintDO::getMemberId,memberId);
        List<GoodsFootprintDO> GoodsFootprintList = goodsFootprintMapper.selectList(wrapper);

        List<GoodsFootprintVO> goodsFootprintVO = new ArrayList<>(GoodsFootprintList.size());

        for (GoodsFootprintDO goodsFootprintDo:GoodsFootprintList) {
            //查询商品信息
            GoodsDO goods = goodsMapper.selectById(goodsFootprintDo.getGoodsId());
            GoodsFootprintVO vo = new GoodsFootprintVO();
            vo.setId(goodsFootprintDo.getId());
            vo.setMemberId(goodsFootprintDo.getMemberId());
            vo.setGoodsId(goodsFootprintDo.getGoodsId());
            vo.setGoodsName(goods.getGoodsName());
            vo.setGoodsPrice(goods.getGoodsPrice());
            vo.setGoodsImage(goods.getGoodsImage());
            vo.setBroTime(goodsFootprintDo.getBroTime());
            goodsFootprintVO.add(vo);
        }
        return goodsFootprintVO;
    }
}
